<template>
  <el-card style="margin: 20px">
    <el-row type="flex" justify="space-between">
      <el-col :span="15">
        <el-row type="flex" justify="start">
          <div style="display: flex">
            学科名称
            <el-input v-model="params.subjectName"></el-input>
          </div>

          <el-button style="margin-left: 10px">清空</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-row>
      </el-col>
      <el-col :span="1">
        <el-button type="success" class="el-icon-edit" @click="adduser"
          >新增学科</el-button
        >
      </el-col>
    </el-row>
    <el-alert
      style="margin-top: 20px"
      :title="`共${counts}条记录`"
      type="info"
      :closable="false"
      show-icon
    >
    </el-alert>

    <el-table style="margin-top: 20px" :data="userlist">
      <el-table-column type="index" label="序号" width="80"> </el-table-column>
      <el-table-column prop="subjectName" label="学科名称" width="80">
      </el-table-column>
      <el-table-column prop="username" label="创建者" width="180">
      </el-table-column>
      <el-table-column prop="addDate" label="创建日期" width="280">
      </el-table-column>
      <el-table-column prop="isFrontDisplay" label="前台是否显示" width="80">
      </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="二级目录" width="80">
      </el-table-column>
      <el-table-column prop="tags" label="标签" width="80"> </el-table-column>
      <el-table-column prop="totals" label="题目数量" width="80">
      </el-table-column>
      <el-table-column prop="date" label="操作" width="280">
        <template #default="{ row }">
          <el-link type="primary" style="margin: 0 10px">学科分类</el-link>
          <el-link type="primary" style="margin: 0 10px">学科标签</el-link>
          <el-link
            type="primary"
            style="margin: 0 10px"
            @click="change(row, row.id)"
            >修改</el-link
          >
          <el-link
            type="primary"
            style="margin: 0 10px"
            @click="removeUser(row, row.id)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin: 25px">
      <el-pagination
        background
        @size-change="params.pagesize"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage3"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
    </div>

    <!-- 弹框 -->

    <el-dialog
      @close="handleClose"
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <div v-if="addView">
        <el-form v-model="form" label-width="120px" ref="formRef">
          <el-form-item label="学科名称">
            <el-input v-model="form.subjectName"></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
              inactive-color="#ff4949"
              active-color="#13ce66"
              v-model="form.isFrontDisplay"
            ></el-switch>
          </el-form-item>
        </el-form>
      </div>

      <div v-if="changeView">
        <el-form v-model="form" label-width="120px" ref="formRef">
          <el-form-item label="学科名称">
            <el-input v-model="form.subjectName"></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
              inactive-color="#ff4949"
              active-color="#13ce66"
              v-model="form.isFrontDisplay"
            ></el-switch>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { list, add, simple, remove, update } from '@/api/hmmm/subjects'
export default {
  data() {
    return {
      id: '',
      changeView: false,
      title: '',
      addView: false,
      options: '',
      keyword: '',
      form: {
        subjectName: '',
        twoLevelDirectory: '',
        totals: '',
        tags: '',
        username: '',
        addDate: '',
        isFrontDisplay: ''
      },
      dialogVisible: false,
      currentPage3: null,
      username: '',
      pages: '',
      input: '',
      counts: null,
      userlist: [],
      params: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      }
    }
  },
  methods: {
    async list() {
      const { data } = await list(this.params)
      this.userlist = data.items
      this.counts = data.counts
      this.pages = data.pages
      console.log(this.userlist)
      console.log(data)
      this.userlist.forEach((item) => {
        if (item.isFrontDisplay === 1) {
          item.isFrontDisplay = '是'
        } else {
          item.isFrontDisplay = '否'
        }
      })
    },
    handleCurrentChange(current) {
      this.params.page = current
      this.list()
    },
    async search() {
      await list(this.params)

      this.list()
    },

    async adduser() {
      this.dialogVisible = true
      this.addView = true
      this.changeView = false
      this.title = '新增学科'
      const { data } = await simple()
      this.options = data
    },

    async change(row) {
      console.log(row)
      this.form = row
      this.id = row.id
      this.form.isFrontDisplay = row.isFrontDisplay

      this.dialogVisible = true
      this.addView = false
      this.changeView = true
      this.title = '修改学科'
      const { data } = await simple()
      this.options = data
    },

    async removeUser(row, id) {
      console.log(id)
      const res = await remove(row, id)
      this.$message.success('删除用户成功')
      console.log(res)
      this.list()
    },

    handleClose() {
      this.dialogVisible = false
      this.$refs.formRef.resetFields()
      this.form = {}
    },
    async submit() {
      console.log(111)
      if (this.title === '新增学科') {
        if (this.form.isFrontDisplay === true) {
          this.form.isFrontDisplay = '1'
        } else {
          this.form.isFrontDisplay = '0'
        }
        await add({
          subjectName: this.form.subjectName,
          isFrontDisplay: this.form.isFrontDisplay
        })
        this.$message.success('新增学科成功')
      } else {
        if (this.form.isFrontDisplay === true) {
          this.form.isFrontDisplay = 1
        } else {
          this.form.isFrontDisplay = 0
        }
        await update({
          subjectName: this.form.subjectName,
          isFrontDisplay: this.form.isFrontDisplay,
          id: this.id
        })
        this.$message.success('编辑学科成功')
      }

      this.dialogVisible = false

      this.list()
    }
  },
  created() {
    this.list()
  }
}
</script>

<style scoped lang="scss"></style>
